﻿Public Class Reportes

    Private oFact As New CFacturacion
    Private oMaestra As New CMaestras

    Private Sub Reportes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.dateFecha1.Value = DateAdd(DateInterval.Month, -1, Date.Today)
        'Carga grillas
        Me.CargarTipo()
        Me.CargarTransporte()
        Me.CargarCiudades()
        Me.CargarClientes()
    End Sub

#Region "Combos"

    Private Sub CargarTipo()
        'Construye tabla para cargar el combo de los tipos
        Dim dt As New DataTable
        dt.Columns.Add("ID")
        dt.Columns.Add("TIPO")
        Dim arrValues(1) As String
        arrValues(0) = "0"
        arrValues(1) = "Todos..."
        dt.Rows.Add(arrValues)
        arrValues(0) = "Importacion"
        arrValues(1) = "Importación"
        dt.Rows.Add(arrValues)
        arrValues(0) = "Exportacion"
        arrValues(1) = "Exportación"
        dt.Rows.Add(arrValues)

        Me.drpTipo.DataSource = dt
        Me.drpTipo.ValueMember = dt.Columns(0).ColumnName
        Me.drpTipo.DisplayMember = dt.Columns(1).ColumnName
    End Sub

    Private Sub CargarTransporte()
        'Construye tabla para cargar el combo
        Dim dt As New DataTable
        dt.Columns.Add("ID")
        dt.Columns.Add("TIPO")
        Dim arrValues(1) As String
        arrValues(0) = "0"
        arrValues(1) = "Todos..."
        dt.Rows.Add(arrValues)
        arrValues(0) = "AEREO"
        arrValues(1) = "Aéreo"
        dt.Rows.Add(arrValues)
        arrValues(0) = "MARITIMO"
        arrValues(1) = "Marítimo"
        dt.Rows.Add(arrValues)

        Me.drpTransporte.DataSource = dt
        Me.drpTransporte.ValueMember = dt.Columns(0).ColumnName
        Me.drpTransporte.DisplayMember = dt.Columns(1).ColumnName
    End Sub

    Public Sub CargarCiudades()
        Dim ds As New DataSet
        ds = Me.oMaestra.GetListadoCiudades()
        If (ds.Tables(0).Rows.Count > 0) Then
            Me.drpOrigen.DataSource = CUtil.AgregarRegistro(ds.Tables(0), "Todas...")
            Me.drpOrigen.ValueMember = ds.Tables(0).Columns(0).ColumnName
            Me.drpOrigen.DisplayMember = ds.Tables(0).Columns(1).ColumnName
        End If
    End Sub

    Private Sub CargarClientes()
        Try
            Dim ds As New DataSet
            ds = oMaestra.GetTabla(CMaestras.Tabla.Cliente)
            If (ds.Tables(0).Rows.Count > 0) Then
                Me.drpCliente.DataSource = CUtil.AgregarRegistro(ds.Tables(0), "Todos...")
                Me.drpCliente.ValueMember = ds.Tables(0).Columns(0).ColumnName
                Me.drpCliente.DisplayMember = ds.Tables(0).Columns(1).ColumnName
            Else
                MessageBox.Show("No se encontraron clientes. Deben ser creados primero", "Clientes", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show("Ocurrió un error al cargar los clientes: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

#End Region

    Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        Try
            Cursor = Cursors.WaitCursor
            Dim desde As String = ""
            Dim hasta As String = ""
            Dim mes As String = ""
            Dim dia As String = ""
            If (Me.dateFecha1.Checked) Then
                mes = Me.dateFecha1.Value.Month
                dia = Me.dateFecha1.Value.Day
                If (mes.Length = 1) Then
                    mes = "0" & mes
                End If
                If (dia.Length = 1) Then
                    dia = "0" & dia
                End If
                desde = Me.dateFecha1.Value.Year & mes & dia
            End If
            If (Me.dateFecha2.Checked) Then
                mes = Me.dateFecha2.Value.Month
                dia = Me.dateFecha2.Value.Day
                If (mes.Length = 1) Then
                    mes = "0" & mes
                End If
                If (dia.Length = 1) Then
                    dia = "0" & dia
                End If
                hasta = Me.dateFecha2.Value.Year & mes & dia
            End If

            Dim arrDatos() As String = Me.oFact.GetReporte(Me.drpTipo.SelectedValue, Me.drpTransporte.SelectedValue, CInt(Me.drpOrigen.SelectedValue), CInt(Me.drpCliente.SelectedValue), desde, hasta)
            Me.lblNumeroFacturas.Text = arrDatos(0)
            Me.lblNumeroConsolidados.Text = arrDatos(1)
            Me.lblNumeroHijas.Text = arrDatos(2)
            Me.lblCantidadKilos.Text = arrDatos(3)
            Me.lblCantidadContenedores.Text = arrDatos(4)

            Cursor = Cursors.Default
        Catch ex As Exception
            Cursor = Cursors.Default
            MessageBox.Show("Ocurrió un error al buscar los consolidados: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub
End Class